<?php

class User_GroupController extends Pldkp_Controller_Action
{
    public function indexAction()
    {
        
        $groups = User_Model_Groups::getAll();
        
        $this->view->groups = $groups;
    }
    
    public function rightsAction()
    {
        $group = User_Model_Groups::getById($this->_request->getParam('group'));
        
        if (!$group) {
            $this->setFlashMessage('Invalid group!');
            $this->redirect(array('action' => 'index'));
        }
        
        $grantedRights = $group->findManyToManyRowset('User_Model_Rights', 'User_Model_GroupsRights');
        
        $rightsTable = new User_Model_Rights();
        $select = $rightsTable->select();
        foreach ($grantedRights as $right) {
            $select->where('id != ?', $right->id);
        }
        $missingRights = $rightsTable->fetchAll($select);
        
        $this->view->group = $group;
        $this->view->grantedRights = $grantedRights;
        $this->view->missingRights = $missingRights;
    }
    
    public function addRightAction()
    {
        $right = User_Model_Rights::getById($this->_request->getParam('right'));
        $group = User_Model_Groups::getById($this->_request->getParam('group'));
        
        if (!$group || !$right) {
            $this->setFlashMessage('Invalid right or group!');
            $this->redirect(array('action' => 'index'));
        }
        
        $form = new Pldkp_Form_Confirm();
        $this->view->form = $form;
        
        if ($this->_request->isPost()) {
            $form->populate($this->_request->getPost());
            if ($form->getValue('yes')) {
                if ($group->addRight($right)) {
                    $this->setFlashMessage('Right granted!');
                    
                } else {
                    $this->setFlashMessage('Could not grant the right!');
                }
            }
            
            $this->redirect(array('action' => 'rights', 'group' => $group->id ));
            return;
        }
        
        $this->view->group = $group;
        $this->view->right = $right;
        $this->view->form = $form;
    }
    
    public function removeRightAction()
    {
        $right = User_Model_Rights::getById($this->_request->getParam('right'));
        $group = User_Model_Groups::getById($this->_request->getParam('group'));
        
        if (!$group || !$right) {
            $this->setFlashMessage('Invalid right or group!');
            $this->redirect(array('action' => 'index'));
        }
        
        $form = new Pldkp_Form_Confirm();
        $this->view->form = $form;
        
        if ($this->_request->isPost()) {
            $form->populate($this->_request->getPost());
            if ($form->getValue('yes')) {
                if ($group->removeRight($right)) {
                    $this->setFlashMessage('Right removed!');
                    
                } else {
                    $this->setFlashMessage('Could not remove the right!');
                }
            }
            
            $this->redirect(array('action' => 'rights', 'group' => $group->id ));
            return;
        }
        
        $this->view->group = $group;
        $this->view->right = $right;
        $this->view->form = $form;
    }
    
    public function addUserAction()
    {
        $group = null;
        $user = null;
        
        $user = User_Model_Users::getById($this->_request->getParam('user'));
        $group = User_Model_Groups::getById($this->_request->getParam('group'));
        
        if (!$group || !$user) {
            $this->setFlashMessage('Invalid user or group!');
            $this->redirect(array('action' => 'index'));
        }
        
        $form = new Pldkp_Form_Confirm();
        $this->view->form = $form;
        
        if ($this->_request->isPost()) {
            $form->populate($this->_request->getPost());
            if ($form->getValue('yes')) {
                if ($group->addUser($user)) {
                    $this->setFlashMessage('User added to group!');
                    
                } else {
                    $this->setFlashMessage('Could not add user to group!');
                }
            }
            
            $this->redirect(array('action' => 'members', 'group' => $group->id ));
            return;
        }
        
        $this->view->group = $group;
        $this->view->user = $user;
        $this->view->form = $form;
    }
    
    public function removeUserAction()
    {
        $group = null;
        $user = null;
        
        $user = User_Model_Users::getById($this->_request->getParam('user'));
        $group = User_Model_Groups::getById($this->_request->getParam('group'));
        
        if (!$group || !$user) {
            $this->setFlashMessage('Invalid user or group!');
            $this->redirect(array('action' => 'index'));
        }
        
        $form = new Pldkp_Form_Confirm();
        $this->view->form = $form;
        
        if ($this->_request->isPost()) {
            $form->populate($this->_request->getPost());
            if ($form->getValue('yes')) {
                if ($group->removeUser($user)) {
                    $this->setFlashMessage('User removed from group!');
                    
                } else {
                    $this->setFlashMessage('Could not remove the user from the group!');
                }
            }
            
            $this->redirect(array('action' => 'members', 'group' => $group->id ));
            return;
        }
        
        $this->view->group = $group;
        $this->view->user = $user;
        $this->view->form = $form;
    }
    
    public function membersAction()
    {
        $group = null;
        $group = User_Model_Groups::getById($this->_request->getParam('group'));
        
        if (!$group) {
            $this->setFlashMessage('Invalid group!');
            $this->redirect(array('action' => 'index'));
        } 
        
        $this->view->group = $group;
        
        // Don't waste time with users for default groups
        if ($group->is_default) {
            return;
        }
        
        $members = $group->findManyToManyRowset('User_Model_Users', 'User_Model_UsersGroups');
        
        // Get everyone not in the group
        // This is the only place where this is relevant so this will not 
        // be placed in the group class
        $usersTable = new User_Model_Users();
        $select = $usersTable->select();
        foreach ($members as $member) {
            $select->where('id != ?', $member->id);
        }
        $nonMembers = $usersTable->fetchAll($select);
        
        $this->view->group = $group;
        $this->view->members = $members;
        $this->view->nonMembers = $nonMembers;   
    }
    
    public function deleteAction()
    {
        $group = null;
        $group = User_Model_Groups::getById($this->_request->getParam('group'));
        
        if (!$group) {
            $this->setFlashMessage('Invalid group!');
            $this->redirect(array('action' => 'index'));
        } 
        
        $form = new Pldkp_Form_Confirm();
        $this->view->form = $form;
        
        if ($this->_request->isPost()) {
            $form->populate($this->_request->getPost());
            if ($form->getValue('yes')) {
                $group->delete();
                $this->setFlashMessage('Group deleted!');
                $this->redirect(array('action' => 'index'));
                return;
            }
            
            $this->redirect(array('action' => 'edit', 'group' => $group->id ));
        }
    }
    
    public function createAction()
    {
        $form = new User_Form_GroupForm();
        
        if ($this->_request->isPost()) {
            $postData = $this->_request->getPost();
            if ($form->isValid($postData)) {
                
                $parentGroup = User_Model_Groups::getById($form->getValue('parent_group_id'));
                
                $group = User_Model_Groups::getNew();
                $group->name = $form->getValue('name');
                $group->is_default = $form->getValue('is_default');
                $group->parent_group_id = ($parentGroup) ? $parentGroup->id : null;
                
                if ($group->save()) {
                    $this->setFlashMessage('Group created!');
                    $this->redirect(array('action' => 'index'));
                } else {
                    $this->setInstantFlashMessage('Could not save the group!');
                    $form->populate($postData);
                }
            } else {
                $form->populate($postData);
            }
        }
        
        $this->view->form = $form;
    }
    
    public function editAction()
    {
        $group = null;
        $group = User_Model_Groups::getById($this->_request->getParam('group'));
        
        if (!$group) {
            $this->setFlashMessage('Invalid group!');
            $this->redirect(array('action' => 'index'));
        } 
        
        $form = new User_Form_GroupForm($group);
        
        if ($this->_request->isPost()) {
            $postData = $this->_request->getPost();
            if ($form->isValid($postData)) {
                
                $parentGroup = User_Model_Groups::getById($form->getValue('parent_group_id'));
                $group->name = $form->getValue('name');
                $group->is_default = $form->getValue('is_default');
                $group->parent_group_id = ($parentGroup) ? $parentGroup->id : null;

                if ($group->save()) {
                    $this->setFlashMessage('Group saved!');
                    $this->redirect(array('action' => 'index'));
                } else {
                    $this->setInstantFlashMessage('Could not save the group, parent group invalid!');
                    $form->populate($postData);
                }
                
            } else {
                $form->populate($postData);
            }
        } else {
            $form->populate(array('name' => $group->name, 
                                  'is_default' => $group->is_default,
                                  'parent_group_id' => $group->parent_group_id));
        }
        
        $this->view->group = $group;
        $this->view->form = $form;
    }
}